//
//  CategoryViewController.m
//  muscleLife
//
//  Created by xalo on 16/4/29.
//  Copyright © 2016年 HeavenAndHell. All rights reserved.
//

#import "CategoryViewController.h"
#import "CategoryCollectionViewCell.h"
#import "DietDetailViewController.h"
#import <MobAPI/MobAPI.h>
@interface CategoryViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property(nonatomic,retain)NSMutableArray *dataSource;
@property(nonatomic,retain)NSArray *childs;

@end

@implementation CategoryViewController

//数据源的懒加载
-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        _dataSource=[NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self collectionviewLayout];
    [self childsValue];
    
}
//取值
-(void)childsValue{
    
    NSArray* array = self.model.childs;
    for (int i = 0; i < array.count; i++) {
        CategoryRootModel* model = [[CategoryRootModel alloc] init];
        [model setValuesForKeysWithDictionary:array[i]];
        [self.dataSource addObject:model];
       }
}
//布局
-(void)collectionviewLayout{
    
    self.collectionView.collectionViewLayout=self.flowLayout;
    self.collectionView.backgroundColor=[UIColor whiteColor];
    self.flowLayout.minimumLineSpacing=10;
    self.flowLayout.minimumInteritemSpacing=10;
    self.flowLayout.itemSize=CGSizeMake(100, 100);
    self.flowLayout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
}

#pragma mark----UICollectionView的代理方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CategoryCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryCollectionViewCell" forIndexPath:indexPath];
    CategoryRootModel* model = self.dataSource[indexPath.row];
    switch (self.indexOfImage) {
        case 0:
            [cell setCellWithModel:model.categoryInfo index:indexPath.row];
            break;
        case 1:
            [cell setCellWithModel:model.categoryInfo index:indexPath.row imageName:@"craft"];
            break;
        case 2:
            [cell setCellWithModel:model.categoryInfo index:indexPath.row imageName:@"cuisine"];
            break;
        case 3:
            [cell setCellWithModel:model.categoryInfo index:indexPath.row imageName:@"crowd"];
            break;
        case 4:
            [cell setCellWithModel:model.categoryInfo index:indexPath.row imageName:@"function"];
            break;
        default:
            break;
    }

    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DietDetailViewController *dietVC= [[DietDetailViewController alloc]init];
    CategoryRootModel* model = self.dataSource[indexPath.row];
    dietVC.model = model.categoryInfo;
    [self.navigationController pushViewController:dietVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
